package com.ytkj.ygAssist.main;

import com.ytkj.ygAssist.server.GetGoodsInfo;
import com.ytkj.ygAssist.server.GetUserBuyServer;
import com.ytkj.ygAssist.server.SelectAssistPublishs;
import com.ytkj.ygAssist.server.util.HttpGetUtil;
import com.ytkj.ygAssist.tools.CacheData;
import com.ytkj.ygAssist.tools.JFrameListeningInterface;
import com.ytkj.ygAssist.tools.MyLog;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/*
 * 提前揭晓服务
 */
public class ForeknowServer {
	public static void getForeknow(final String goodsID, String period, String EIdx,
								   final JFrameListeningInterface foreknowInterface) {
		if (CacheData.getGoodsNameCacheDate(goodsID) == null) {
			if (!GetGoodsInfo.getGoodsInfoByGoodsID(goodsID)) {
				foreknowInterface.setFrameText("setGoodsNameError", null);
				return;
			}
		}
		CacheData.setGoodsTreeListCacheDate(goodsID);
		foreknowInterface.setFrameText("setGoodsName", CacheData.getGoodsNameCacheDate(goodsID));

		if (period != null) {
			if (CacheData.getSelectCacheDate(goodsID, period) == null) {
				SelectAssistPublishs.getYungouPublishs(goodsID, period, "1");
				if (CacheData.getSelectCacheDate(goodsID, period) == null) {
					String[] content = GetGoodsInfo.getGoodsPeriodInfo(goodsID, period);
					if (content != null) {
						String[] text2 = new String[] { goodsID, period, "", "", "", "", "" };
						foreknowInterface.setFrameListeningText("0", text2);
						if (content[0].equals("1")) {
							String[] text = new String[] { goodsID, period, "正在云购中", "正在云购中", "正在云购中", "正在云购中",
									"正在云购中" };
							foreknowInterface.setFrameListeningText("0", text);
						} else if (content[0].equals("2")) {
							GetUserBuyServer.getUserBuyServer(content[1]).GetGoodsPeriodInfo(goodsID, period,
									content[1], false);
						} else if (content[0].equals("3")) {
							GetUserBuyServer.getUserBuyServer(content[1]).GetGoodsPeriodInfo(goodsID, period,
									content[1], true);
						} else {
							foreknowInterface.setFrameText("selectError", goodsID + "___" + period + "___0");
						}
					} else {
						foreknowInterface.setFrameText("selectError", goodsID + "___" + period + "___0");
					}
				} else {
					foreknowInterface.setFrameListeningText("0", CacheData.getSelectCacheDate(goodsID, period));
				}
			} else {
				foreknowInterface.setFrameListeningText("0", CacheData.getSelectCacheDate(goodsID, period));
			}
		} else {
			boolean isHavaData = true;
//			String text[] = GetGoodsInfo.shopCartNew(CacheData.getGoodsInfoCacheDate(goodsID)[2],
//					HttpGetUtil.getHttpClient());
//			String text[] = GetGoodsInfo.shopCartNewByHtml(goodsID,
//					HttpGetUtil.getHttpClient());
			String text[] = GetGoodsInfo.getGoodsSalingByGoodsID(goodsID,
					HttpGetUtil.getHttpClient());
			ConcurrentHashMap<String, String[]> selectCacheDateMap = CacheData.getSelectCacheDate(goodsID);
			try {
				int newestPeriod = Integer.parseInt(text[1]);
				foreknowInterface.setFrameText("initTable", Integer.toString(newestPeriod));
				int EIdxInt = Integer.parseInt(EIdx);
				for (int i = 1; i < EIdxInt && i < newestPeriod; i++) {
					String[] cacheDateStr = selectCacheDateMap.get(Integer.toString(newestPeriod - i));
					if (cacheDateStr == null) {
						isHavaData = false;
					} else {
						foreknowInterface.setFrameListeningText(null, cacheDateStr);
					}
				}
			} catch (Exception e) {
			}
			int price = Integer.parseInt(text[3]);
			CacheData.setGoodsPriceCacheDate(goodsID, price);
			if (!isHavaData) {
				SelectAssistPublishs.getYungouPublishs(goodsID, "0", EIdx);
			}
			List<Map<String, String>> contentList = GetGoodsInfo.getBarcodeRaffListByGoodsID(goodsID, EIdx);
			if (contentList != null) {
				for (final Map<String, String> map : contentList) {
					final String codePeriod = map.get("codePeriod");
					String[] SelectText = selectCacheDateMap.get(codePeriod);
					final String codeRNO = map.get("codeRNO");
					if (SelectText != null) {
						if (!codeRNO.equals("") && !SelectText[2].equals(codeRNO)) {
							MyLog.outLog("计算结果错误了", goodsID, codePeriod, "原", SelectText[2], "正确", codeRNO);
							SelectAssistPublishs.deleteYungouPublish(goodsID, codePeriod);
							CacheData.removeSelectCacheDate(goodsID, codePeriod);
							SelectText = null;
						}
					}
					if (SelectText == null) {
						Thread thread = new Thread(new Runnable() {
							public void run() {
								if (map.get("codeState").equals("1")) {
									String[] text = new String[] { goodsID, codePeriod, "正在云购中", "正在云购中", "正在云购中",
											"正在云购中", "正在云购中" };
									foreknowInterface.setFrameListeningText(null, text);
								} else {
									String codeID = map.get("codeID");
									if (codeRNO.equals("")) {
										GetUserBuyServer.getUserBuyServer(codeID).GetGoodsPeriodInfo(goodsID,
												codePeriod, codeID, false);
									} else {
										GetUserBuyServer getUserBuyServer = GetUserBuyServer.getUserBuyServer(codeID);
										getUserBuyServer.setBarcodernoInfo(codeRNO, map.get("userName"),
												map.get("userWeb"));
										getUserBuyServer.GetGoodsPeriodInfo(goodsID, codePeriod, codeID, false);
									}
								}
							}
						});
						thread.start();
					} else {
						foreknowInterface.setFrameListeningText(null, SelectText);
					}
				}
			} else {
				foreknowInterface.setFrameText("goodsoldOut", null);
			}

		}
	}
}
